/*
 PureMVC AS3 Demo - Flex CafeTownsend
 Copyright (c) 2007-08 Michael Ramirez <michael.ramirez@puremvc.org>
 Parts Copyright (c) 2005-07 Adobe Systems, Inc. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
/*
Copyright 2011 The Apache Software Foundation

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package idv.takeshi.view
{
    import idv.takeshi.ApplicationFacade;
    import idv.takeshi.model.*;
    import idv.takeshi.view.components.*;
    
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.mediator.Mediator;
    
    /**
     * A Mediator for interacting with the top level Application.
     * 
     * <P>
     * In addition to the ordinary responsibilities of a mediator
     * the MXML application (in this case) built all the view components
     * and so has a direct reference to them internally. Therefore
     * we create and register the mediators for each view component
     * with an associated mediator here.</P>
     * 
     * <P>
     * Then, ongoing responsibilities are: 
     * <UL>
     * <LI>listening for events from the viewComponent (the application)</LI>
     * <LI>sending notifications on behalf of or as a result of these 
     * events or other notifications.</LI>
     * <LI>direct manipulating of the viewComponent by method invocation
     * or property setting</LI>
     * </UL>
     */
    public class ApplicationMediator extends Mediator implements IMediator
    {
        // Cannonical name of the Mediator
        public static const NAME:String = "ApplicationMediator";
		// available values for the main viewstack
		// defined as contants to help uncover errors at compile time instead of run time
		public static const EMPLOYEE_LOGIN : Number =	0;
		public static const EMPLOYEE_LIST : Number =	1;
		public static const EMPLOYEE_DETAIL : Number =	2;
        
        /**
         * Constructor. 
         * 
         * <P>
         * On <code>applicationComplete</code> in the <code>Application</code>,
         * the <code>ApplicationFacade</code> is initialized and the 
         * <code>ApplicationMediator</code> is created and registered.</P>
         * 
         * <P>
         * The <code>ApplicationMediator</code> constructor also registers the 
         * Mediators for the view components created by the application.</P>
         * 
         * @param object the viewComponent (the CafeTownsend instance in this case)
         */
        public function ApplicationMediator( viewComponent:Object ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( NAME, viewComponent );

            // Create and register Mediators for the Employee
            // components that were instantiated by the mxml application
            facade.registerMediator( new PersonDetailMediator( app.personDetail ) );    
            facade.registerMediator( new PersonListMediator( app.personList ) );    
			facade.registerMediator( new PersonLoginMediator( app.personLogin ) );
            
            // retrieve and cache a reference to frequently accessed proxys
            personProxy = PersonProxy( facade.retrieveProxy( PersonProxy.NAME ) );
//			userProxy = UserProxy( facade.retrieveProxy( UserProxy.NAME ) );

        }

        
        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            
            return [ ApplicationFacade.VIEW_PERSON_LOGIN,
					 ApplicationFacade.VIEW_PERSON_LIST,
					 ApplicationFacade.VIEW_PERSON_DETAIL,
					 ApplicationFacade.APP_LOGOUT,
					 ApplicationFacade.UPDATE_PERSON
					];
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
                
				case ApplicationFacade.VIEW_PERSON_LOGIN:
					app.vwStack.selectedIndex = EMPLOYEE_LOGIN;
					break;
				
				case ApplicationFacade.VIEW_PERSON_LIST:
					app.vwStack.selectedIndex = EMPLOYEE_LIST;
					sendNotification(ApplicationFacade.LOAD_PERSONS);
					break;
				
				case ApplicationFacade.VIEW_PERSON_DETAIL:
					app.vwStack.selectedIndex = EMPLOYEE_DETAIL;
					break;
				
				case ApplicationFacade.APP_LOGOUT:
					app.vwStack.selectedIndex = EMPLOYEE_LOGIN;
					break;
				
				case ApplicationFacade.UPDATE_PERSON:
					app.vwStack.selectedIndex = EMPLOYEE_DETAIL;
					break;
					
            }
        }

        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return app the viewComponent cast to CafeTownsend
         */
        protected function get app():Index
		{
            return viewComponent as Index
        }

		// Cached references to needed proxies
		private var personProxy:PersonProxy;
//		private var userProxy:UserProxy;
    }
}
